#from myelin.Ns import MYELINS
from nodetypes.services.sparql.UrlService import Service as SparqlService
from nodetypes.formats.mml.Events import EventHandler
from nodetypes.services import Query

from nodetypes.services.RdfQueryConfig import Serializer as ConfigSerializer

from forms import Commit

class EventHandler(EventHandler):
    
    newQuerySearcher        = SparqlService
    newConfigSerializer     = ConfigSerializer
    
    newCommitForm           = Commit.Form
    
    query                   = Query.Query()
    
    def process(self, context, source):
        eventData = context.event.data
        eventData.config = self.newConfigSerializer().deserialize(source.asInputSource())
        
        params, errors = self.newQuerySearcher().fetchParams(
            context,
            source,
            {}, {}
        )
        
        eventData = context.event.data
        if errors:
            eventData.status = u'failure'
            eventData.status_type = u'search-param-errors'
            
            context.request.status = Status.HTTP_BAD_REQUEST
            
        else:
            eventData.status = u'success'
            eventData.status_type = u'reset-success'
            
        eventData.form = self.newCommitForm()
        eventData.params = params
        eventData.errors = errors
        
        eventData.stage = 0
        eventData.submits = context.transform.submitEvents[0]
        
        context.model = context.nodeType.retrieveModel(context)
        
        return self.newDocument().createDocument(context, source)